#include <nifti/nifti1.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std;

#define MIN_HEADER_SIZE 348
#define NII_HEADER_SIZE 352

int main()
{
	nifti_1_header hdr;
	nifti1_extender pad={0,0,0,0};
	FILE *fp;
	int ret;

	/********** fill in the minimal default header fields */
	memset((void *)&hdr,0,sizeof(hdr));
	hdr.sizeof_hdr = MIN_HEADER_SIZE;
	hdr.dim[0] = 3;
	hdr.dim[1] = 512;
	hdr.dim[2] = 512;
	hdr.dim[3] = 512;
	hdr.datatype = NIFTI_TYPE_FLOAT32;
	hdr.bitpix = 32;
	hdr.pixdim[1] = 1;
	hdr.pixdim[2] = 1;
	hdr.pixdim[3] = 1;
	hdr.vox_offset = (float) NII_HEADER_SIZE;
	hdr.scl_slope = 0;
	hdr.xyzt_units = NIFTI_UNITS_MM;
	strncpy(hdr.magic, "n+1\0", 4);


	/********** write first 348 bytes of header   */
	string name("volume.nifti");
	fp = fopen(name.c_str(),"w");
	if (fp == NULL) {
		fprintf(stderr, "\nError opening header file name.nii for write\n");
		exit(1);
	}
	ret = fwrite(&hdr, MIN_HEADER_SIZE, 1, fp);
	if (ret != 1) {
		fprintf(stderr, "\nError writing header file name.nii\n");
		exit(1);
	}


	/********** write extender pad and image data   */
	ret = fwrite(&pad, 4, 1, fp);
	if (ret != 1) {
		fprintf(stderr, "\nError writing header file extension pad name.nii\n");
		exit(1);
	}
	void* buffer;
	ret = fwrite(buffer, (size_t)(hdr.bitpix/8), hdr.dim[1]*hdr.dim[2]*hdr.dim[3], fp);
	if (ret != hdr.dim[1]*hdr.dim[2]*hdr.dim[3]) {
		fprintf(stderr, "\nError writing data to phi.nii\n");
		exit(1);
	}

	fclose(fp);

	delete[] buffer;

	return 1;
}